package edu.sias.hyznxfapi.controller;

import edu.sias.common.constant.Constants;
import edu.sias.common.core.domain.AjaxResult;
import edu.sias.common.core.domain.entity.SysUser;
import edu.sias.common.utils.SecurityUtils;
import edu.sias.framework.web.service.SysLoginService;
import edu.sias.hyznxfapi.face.baidu.utils.BaiduAiUtils;
import edu.sias.hyznxfapi.vo.ImageVo;
import edu.sias.system.service.ISysUserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/baidu/face")
@Slf4j
public class BaiduApiFaceController {
    private final BaiduAiUtils baiduAiUtils = new BaiduAiUtils();  // 将 BaiduAiUtils 实例化移到类成员变量



    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysLoginService loginService;

    /**
     * 人脸注册及人脸更新
     * @param imageVo
     * @return
     * @throws Exception
     */
    @PostMapping("/registration")
    public AjaxResult upload(@RequestBody ImageVo imageVo) throws Exception {
        String image = imageVo.getImageUrl();
        String name = SecurityUtils.getUsername();
        SysUser sysUser = userService.selectUserByUserName(name);
//
//        BaiduAiUtils baiduAiUtils = new BaiduAiUtils();  // 实例化一次 BaiduAiUtils

        if (!baiduAiUtils.faceCheck(image)) {
            return AjaxResult.error("人脸校验失败");
        }

        if (sysUser.getFaceCode() == 1) {
            // 已经导入过人脸信息了
            if (baiduAiUtils.faceUpdate(name, image) && userService.add(name, image)) {
                return AjaxResult.success("人脸更新成功");
            } else {
                return AjaxResult.error("人脸更新失败");
            }
        } else {
            if (baiduAiUtils.faceRegister(name, image) && userService.add(name, image)) {
                return AjaxResult.success("人脸注册成功");
            } else {
                return AjaxResult.error("人脸注册失败");
            }
        }
    }



    /**
     * 删除用户，而不是删除单个人脸
     * @return
     */
    @GetMapping("/delete")
    public AjaxResult delete() {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        SysUser sysUser = userService.selectUser(name);
        int faceCode = sysUser.getFaceCode();

        if (faceCode == 0) {
            return AjaxResult.error("尚未录入，请先录入");
        }
        if ( baiduAiUtils.faceDeleteUser(name) && userService.delete(name)) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }


    /**
     * 记得去hyzn-framework\src\main\java\edu\sias\framework\config\SecurityConfig.java
     * 的configure方法中放行
     * @param imageVo
     * @return
     * @throws Exception
     */
    @PostMapping("/faceLogin")
    public AjaxResult find(@RequestBody ImageVo imageVo) throws Exception {
        //人脸注册，需要两个参数，userId和base64
        String image = imageVo.getImageUrl();
        System.out.println("image = " + image);
        String userId =  baiduAiUtils.faceSearch(image);
        AjaxResult ajax = AjaxResult.success();
        //获取后端数据库中本人存的图片编码
 //       SysUser sysUser = userService.selectUser(userId);
//        String password = sysUser.getPassword();
        String token = loginService.faceLogin(userId);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

}
